package work.soho.proxy.aliyun.parts;

import com.aliyuncs.exceptions.ClientException;
import work.soho.proxy.PartsConfigure;
import work.soho.proxy.ProxyApplication;

import java.io.IOException;
import java.util.*;

public class PartsManage {
    private static LinkedHashMap<String, IPart> partsList = null;

    /**
     * 配置信息
     */
    private static PartsConfigure partsConfigure;

    public static void setConfig(PartsConfigure config) {
        partsConfigure = config;
    }

    static {
        try {
            getPartsList();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取所有的部件
     *
     * @return
     */
    public static LinkedHashMap<String, IPart> getPartsList() throws IOException {
        if(partsList == null) {
            partsList = new LinkedHashMap<>();
            partsList.put(ImportKeyPairPart.class.getName(), new ImportKeyPairPart());
            partsList.put(VpcPart.class.getName(), new VpcPart());
            partsList.put(SwitchPart.class.getName(), new SwitchPart());
            partsList.put(SecurityGroupPart.class.getName(), new SecurityGroupPart());
            partsList.put(ServerInstancePart.class.getName(), new ServerInstancePart());
            partsList.put(DdnsLanPart.class.getName(), new DdnsLanPart());
            partsList.put(DdnsWanPart.class.getName(), new DdnsWanPart());
            //迭代添加配置信息
            Iterator it = partsList.entrySet().iterator();
            while (it.hasNext()) {
                Map.Entry<String, IPart> entry = (Map.Entry<String, IPart>) it.next();
                entry.getValue().setConfig(ProxyApplication.configManage.getPartsConfigure());
            }
        }
        return partsList;
    }

    /**
     * 检查创建服务
     */
    public static void check() throws AliyunPartsException, ClientException, IOException {
        Iterator it = getPartsList().entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry<String, IPart> entry = (Map.Entry<String, IPart>) it.next();
            IPart part = entry.getValue();
            if(!part.isExist()) {
                part.create();
            } else {
                part.configure();
            }
        }
    }

    /**
     * 检查指定组件
     *
     * @param parts
     */
    public static void check(List<String> parts) {
        for (String partName: parts) {
            try {
                IPart part = getPartsList().get(partName);
                if(!part.isExist()) {
                    part.create();
                } else {
                    part.configure();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 切换服务器
     *
     * @throws AliyunPartsException
     * @throws InterruptedException
     */
    public static void switchServer() throws AliyunPartsException, InterruptedException, ClientException, IOException {
        IPart part = partsList.get(ServerInstancePart.class.getName());
        part.drop();
        Thread.sleep(1000 * 3);
        part.create();
    }

    /**
     * 删除服务器
     */
    public static void dropServer() {
        IPart part = partsList.get(ServerInstancePart.class.getName());
        part.drop();
    }

    /**
     * 根据名字获取组件
     *
     * @param name
     * @return
     */
    public static IPart getPartByName(String name) {
        System.out.println(name);
        return partsList.get(name);
    }

//    /**
//     * 启动socket5代理
//     */
//    public static void startSocket5() {
//        try {
//            ServerInstancePart serverInstancePart = (ServerInstancePart) getPartByName(ServerInstancePart.class.getName());
//
//            String command = "ssh -o StrictHostKeyChecking=no -qTNf -D 0.0.0.0:7070 root@" + serverInstancePart.getWIp();
//            System.out.println("runing : " + command);
//            Runtime.getRuntime().exec(command);
//            System.out.println("bind success 0.0.0.0:7070");
//        } catch (Exception e) {
//            System.out.println("exception:" + e.getMessage());
//        }
//    }
}
